package com.kaoshi.myweb.webtest.jpatest.entity;

import java.util.List;
import java.util.Set;

import javax.annotation.Generated;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.Valid;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.annotations.Proxy;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;

@Entity
@Table(name = "ss_user")
/*
 * 1：在实体类上使用@NamedQuery（注意：@NamedQuery中query参数中的表名，填写 模型名称，字段名称  填写 模型的属性名称 ）
 * 2：在自己实现的DAO的Repository接口里面定义一个同名的方法，
 * public List<UserJpaModel> findByName(String name);
 * 3：然后就可以使用了，Spring会先找是否有同名的NamedQuery
 */
@NamedQuery(name="UserJpaModel.findByName", query = "select o.id, o.password from UserJpaModel o where o.name = ?1")
@Proxy(lazy = true, proxyClass = UserJpaModel.class)
public class UserJpaModel {
	
	@Column(name = "login_name")
	private String loginName;
	
	private String password;
	private String salt;
	private String name;
	private String email;
	private String status;
	
	/*
	 * 定义的实体中 必须包含 @Id，唯一列；沒有的話，会报 org.hibernate.AnnotationException: 异常
	 * Hibernate is an intermediate to adress SQL databases,
	 *  and since each row in a sql database should have a unique identifier, 
	 *  hibernate forces you to define one.
	 */
	@Id
	@GeneratedValue
	protected Long id;
	
//	@Valid
//	@OneToMany(cascade = CascadeType.ALL, mappedBy="userJpaModel",  orphanRemoval = true)
//	private Set<TaskJpaModel> taskJpaModel;
	
	public UserJpaModel(){}
	
	public UserJpaModel(Long id) {
		this.id = id;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	@NotBlank
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}

	@NotBlank
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Email
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

//	public Set<TaskJpaModel> getTaskJpaModel() {
//		return taskJpaModel;
//	}
//
//	public void setTaskJpaModel(Set<TaskJpaModel> taskJpaModel) {
//		this.taskJpaModel = taskJpaModel;
//	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}
